Fix zombie connection when TLS handshake fails in HTTP proxy#1049
Open
baizhufbb wants to merge 4 commits intoencode:masterfrom
Open
Fix zombie connection when TLS handshake fails in HTTP proxy#1049baizhufbb wants to merge 4 commits intoencode:masterfrom
baizhufbb wants to merge 4 commits intoencode:masterfrom
Conversation
When using an HTTP proxy to connect to HTTPS servers, if the TLS handshake fails after CONNECT succeeds, the connection remains in ACTIVE state and never gets cleaned up, occupying the connection pool forever. This fix ensures the connection is properly closed when TLS fails, preventing Pool timeout issues.
Apply the same fix to the synchronous version to keep async/sync in sync.
25bf898 to
35ddb37
Compare
Author
|
Detailed analysis: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using an HTTP proxy to connect to HTTPS servers, if the TLS handshake fails (e.g., proxy timeout, node switching) after the CONNECT request succeeds, the connection gets stuck in
ACTIVEstate and never gets cleaned up. This results in a zombie connection that permanently occupies the connection pool.Symptoms:
is_closed() = False(connection state is ACTIVE, not CLOSED)has_expired() = False(only checks IDLE state connections)Pool timeoutinfinite loopRoot Cause
Execution flow when TLS handshake fails:
ACTIVEConnectError: EndOfStream)aclose()is calledACTIVEis_closed()returnsFalse(state is ACTIVE)has_expired()returnsFalse(only checks ifstate == IDLE)Solution
This PR adds a try-except block around
start_tls()to ensure cleanup on TLS failures:Alternative Approaches Considered
Option 1: Outer try-except
Following the pattern in AsyncHTTP11Connection.handle_async_request(), we could wrap the entire tunnel establishment in an outer try-except.
Option 2: Fix has_expired() logic
Check for ACTIVE state with readable socket:
Pros: No need to modify http_proxy.py.
Cons: May incorrectly close healthy ACTIVE connections, broader impact.
Verification
Before fix (zombie connection):
20:28:08.895 - start_tls.failed exception=ConnectError(EndOfStream())
20:28:08.896 - state=ACTIVE, is_readable=True
20:28:08.896 - Connection not cleaned: closed=False, expired=False, idle=False
20:28:08.897 - Pool: 1/1 (full)
20:28:11.412 - Pool timeout (infinite loop)
After fix (proper cleanup):
08:42:53.481 - start_tls.failed exception=ConnectError(EndOfStream())
08:42:53.482 - close.started
08:42:53.482 - close.complete
08:42:53.482 - is_closed=True
08:42:53.482 - Removing closed connection. Pool size before: 1
08:42:53.483 - Pool size after removing closed: 0
08:42:54.487 - Creating new connection. Pool: 0/1
08:42:57.386 - start_tls.failed (retry succeeded)
Test environment:
Related
Checklist